<?php

/**
 * 行为分析-事件
 *
 * @author meng.deng
 */

require_once 'CommonController.php';

class Admin_ActEventController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_eventModel = new Application_Model_ActEvent();
        $this->_eventInfoModel = new Application_Model_ActEventInfo();
        $this->_productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $this->_filePath = 'http://uaa.250y.com:8180/action';
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {

        
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['expType'] = trim($this->_request->get('expType'))!='' ? trim($this->_request->get('expType')) : 'test';
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['dateType'] = trim($this->_request->get('dateType'))!='' ? trim($this->_request->get('dateType')) : 1;
        $vars['doType'] = trim($this->_request->get('doType'))!='' ? trim($this->_request->get('doType')) : 1;
        $vars['startTime'] = trim($this->_request->get('startTime'))!='' ? trim($this->_request->get('startTime')) : date('Y-m-d',strtotime("0 day"));
        $vars['version'] = trim($this->_request->get('version'))!='' ? trim($this->_request->get('version')) : '';
        $vars['userSign'] = trim($this->_request->get('userSign'))!='' ? trim($this->_request->get('userSign')) : '';
        $vars['eventType'] = trim($this->_request->get('eventType'))!='' ? trim($this->_request->get('eventType')) : '';
        $vars['pageSize'] = trim($this->_request->get('pageSize'))!='' ? intval(trim($this->_request->get('pageSize'))) : 100;
        $vars['eventData01'] = trim($this->_request->get('eventData01'))!='' ? trim($this->_request->get('eventData01')) : '';
        $vars['eventSort'] = trim($this->_request->get('eventSort'))!='' ? trim($this->_request->get('eventSort')) : 'SORT_DESC';
        
        if ($this->_request->isPost()) {
            if (!$vars['expType']){
                echo $this->view->message('请选择操作环境!') ;
                exit;
            }
            if ((!$vars['startTime'])||(!preg_match('/^[\d]{4}-[\d]{1,2}-[\d]{1,2}$/', $vars['startTime']))){
                echo $this->view->message('请选择日志日期!') ;
                exit;
            }
            if (!$vars['osName']){
                echo $this->view->message('请选择操作系统!') ;
                exit;
            }
            if (!$vars['version']){
                echo $this->view->message('请输入APP版本号!') ;
                exit;
            }
            if ($vars['userSign'] && strlen($vars['userSign'])!=32){
                echo $this->view->message('userSign输入有误!') ;
                exit;
            }
            if ($vars['eventData01'] && strlen($vars['eventData01'])>4){
                echo $this->view->message('参数01输入有误,如：页面id或操作id!') ;
                exit;
            }
        }
        
        
        
        
        
        $urls = $vars;
        
        //事件类型定义
        $sql = "SELECT eventId,eventName,eventNote FROM `{$this->_eventModel->getTableName()}`  ORDER BY eventId ASC";
        $eventRows = $this ->_eventModel ->getAdapter()->fetchAll($sql);
        $reEventRows = array();
        foreach ($eventRows as $row){
            $reEventRows[$row['eventId']]['eventId'] = $row['eventId'];
            $reEventRows[$row['eventId']]['eventName'] = $row['eventName'];
            $reEventRows[$row['eventId']]['eventNote'] = $row['eventNote'];
        }
        //dump($reEventRows);
        
        //事件信息定义
        $sql = "SELECT eventId,eventInfoId,eventInfoName,eventInfoNote,eventFlag,eventSort FROM `{$this->_eventInfoModel->getTableName()}`  ORDER BY eventSort DESC";
        $eventInfoRows = $this ->_eventInfoModel ->getAdapter()->fetchAll($sql);
        $reEventInfoRows = array();
        foreach ($eventInfoRows as $row){
            $reEventInfoRows[$row['eventId'].'-'.$row['eventInfoId']]['eventId'] = $row['eventId'];
            $reEventInfoRows[$row['eventId'].'-'.$row['eventInfoId']]['eventInfoId'] = $row['eventInfoId'];
            $reEventInfoRows[$row['eventId'].'-'.$row['eventInfoId']]['eventInfoName'] = $row['eventInfoName'];
            $reEventInfoRows[$row['eventId'].'-'.$row['eventInfoId']]['eventInfoNote'] = $row['eventInfoNote'];
            $reEventInfoRows[$row['eventId'].'-'.$row['eventInfoId']]['eventFlag'] = $row['eventFlag'];
            $reEventInfoRows[$row['eventId'].'-'.$row['eventInfoId']]['eventSort'] = $row['eventSort'];
        }
        //dump($reEventInfoRows);
        
        
        
        //文本日志解析
        $selDate = $vars['startTime'] ? explode('-',$vars['startTime']) : array();
        $url = "{$this->_filePath}/{$vars['expType']}/log/2/{$this->_productCode}/{$vars['osName']}/{$vars['version']}/{$selDate[0]}/{$selDate[1]}-{$selDate[2]}.log";
        //dump($url);
        $dataArray = array();
        $dataArray = @file($url);
        //dump($dataArray);
        
        $userSign = '';
        $addTime = '';
        $eventIp = '';
        
        $eventData01 = '';
        $eventData02 = '';
        $eventData03 = '';
        $eventData04 = '';
        
        $lineNum = 0;
        $lineDataStartNum = 1;
        
        $reDataArray = array();
        
        if ($dataArray){
            foreach ($dataArray as $key => $row){
                $lineNum = $lineNum+1;
                if (trim($row) == ''){
                    //数据包头信息   分割行
                    $lineDataStartNum = $lineNum+1;
                }
                if ($lineNum == $lineDataStartNum){
                    $bagRows = $row!='' ? explode('	',$row) : array();
                    if (count($bagRows)==6){
                        $userSign = isset($bagRows[1]) ? trim($bagRows[1]) : '';
                        $addTime = isset($bagRows[3]) ? trim($bagRows[3]) : '';
                        $eventIp = isset($bagRows[4]) ? trim($bagRows[4]) : '';
                    }
                    //dump($bagRows);
                } elseif ($lineNum == $lineDataStartNum-1){
                    $userSign = '';
                    $addTime = '';
                    $eventIp = '';
                } else{
                    //dump($userSign);
                    $eventRows = $row!='' ? explode('	',$row) : array();
                    //dump($eventRows);
                    //事件Id
                    $eventId = isset($eventRows[0]) ? trim($eventRows[0]) : '';
                    
                    //来源页面Id
                    $eventPageId = isset($eventRows[1]) ? trim($eventRows[1]) : '';
                    
                    //事件发生时间
                    $eventTime = isset($eventRows[2]) ? trim($eventRows[2]) :'';
                    
                    //事件参数
                    $eventData01 = isset($eventRows[3]) ? trim($eventRows[3]) :'';
                    
                    //注意：页面跳转事件[1004]
                    if (trim($eventId)=='1004'){
                        $eventData02 = isset($eventRows[5])? trim($eventRows[5]):'';
                        $eventData03 = isset($eventRows[4])? trim($eventRows[4]):'0';
                    } else{
                        $eventData02 = isset($eventRows[4])? trim($eventRows[4]):'';
                        $eventData03 = '0';
                    }
                    if($userSign!=''&&$eventId!=''){
                        $reDataArray[$key]['userSign'] = $userSign;
                        $reDataArray[$key]['addTime'] = $addTime;
                        $reDataArray[$key]['eventIp'] = $eventIp;
                        $reDataArray[$key]['eventId'] = $eventId;
                        $reDataArray[$key]['eventName'] = isset($reEventRows[$eventId]['eventName']) ? $reEventRows[$eventId]['eventName'] : '';
                        $reDataArray[$key]['eventNote'] = isset($reEventRows[$eventId]['eventNote']) ? $reEventRows[$eventId]['eventNote'] : '';
                        $reDataArray[$key]['eventPageId'] = $eventPageId;
                        $reDataArray[$key]['eventPageName'] = isset($reEventInfoRows['1004'.'-'.$eventPageId]['eventInfoName']) ? $reEventInfoRows['1004'.'-'.$eventPageId]['eventInfoName'] : '';
                        $reDataArray[$key]['eventTime'] = $eventTime;
                        $reDataArray[$key]['eventDate'] = date('Y-m-d H:i:s',intval($eventTime));
                        $reDataArray[$key]['eventData01'] = $eventData01;
                        $reDataArray[$key]['eventData02'] = $eventData02;
                        $reDataArray[$key]['eventInfoName'] = '';
                        $reDataArray[$key]['eventInfoNote'] = '';
                        if ($eventId=='1004' || $eventId=='1005'){
                            $reDataArray[$key]['eventInfoName'] = isset($reEventInfoRows[$eventId.'-'.$eventData01]['eventInfoName']) ? $reEventInfoRows[$eventId.'-'.$eventData01]['eventInfoName'] : '';
                            $reDataArray[$key]['eventInfoNote'] = isset($reEventInfoRows[$eventId.'-'.$eventData01]['eventInfoNote']) ? $reEventInfoRows[$eventId.'-'.$eventData01]['eventInfoNote'] : '';
                        }
                        $reDataArray[$key]['eventData03'] = $eventData03;
                        $reDataArray[$key]['eventData04'] = $eventData04;
                        //程序自动验证
                        $reDataArray[$key]['error'] = 0;
                        //点击事件
                        if ($eventId == '1005') {
                            //加入购物车
                            if ($eventData01 =='1003'){
                                //$eventData02 ="1020|1|优雅蕾丝亲肤时尚打底衫两件组合装|019911363-200TZ[012111313-088S,012111313-088XL]";
                                if (!preg_match('/^[1-9]{1}[0-9]{3}\|[1-9]+\|[^\|]+\|[^\|]+$/', $eventData02)){
                                    $reDataArray[$key]['error'] = 1;
                                }
                            }
                            //立即购买
                            if ($eventData01 =='1004'){
                                //$eventData02 ="1020|1|优雅蕾丝亲肤时尚打底衫两件组合装|019911363-200TZ[012111313-088S,012111313-088XL]";
                                if (!preg_match('/^[1-9]{1}[0-9]{3}\|[1-9]+\|[^\|]+\|[^\|]+$/', $eventData02)){
                                    $reDataArray[$key]['error'] = 1;
                                }
                            }
                            //提交订单
                            if ($eventData01 =='1007'){
                                //$eventData02 ="|{1020|优雅蕾丝亲肤时尚打底衫两件组合装|019911363-200TZ[012111313-088S,012111313-088XL]|69.00|1,1007|abc|23445|20.00|2,1007|abc|23445|20.00|2}|";
                                if (!preg_match('/\|\{[1-9]{1}[0-9]{3}\|[^\|]+\|[^\|]+\|[\d]+(\.[\d]+){0,1}\|[1-9]+(,[1-9]{1}[0-9]{3}\|[^\|]+\|[^\|]+\|[\d]+(\.[\d]+){0,1}\|[1-9]+)*\}\|/', $eventData02)){
                                    $reDataArray[$key]['error'] = 1;
                                }
                            }
                            //执行搜索
                            if ($eventData01 =='1011'){
                                //$eventData02 ="搜索条件|条件来源id";
                                if (!preg_match('/^[^\|]+\|[0-2]{1}$/', $eventData02)){
                                    $reDataArray[$key]['error'] = 1;
                                }
                            }
                            
                        }
                        //页面跳转
                        if ($eventId == '1004') {
                            //商品详情
                            if ($eventData01 =='1007'){
                                //$eventData02 ="优雅别致两穿领修身长袖打底衫T恤|032011402";
                                if (!preg_match('/^[^\|]+\|[^\|]+$/', $eventData02)){
                                    $reDataArray[$key]['error'] = 1;
                                }
                            }
                            //专题
                            if ($eventData01 =='1020'){
                                //$eventData02 = "   ";
                                if (!preg_match('/^[^\|]+$/', trim($eventData02))){
                                    $reDataArray[$key]['error'] = 1;
                                }
                            }
                        }
                        
                        
                        
                        
                        
                        
                    }
                
                }
                
            }
        
            //dump($reDataArray);
            //过渡
            foreach ($reDataArray as $key => $row){
                if ($vars['userSign']&&$row['userSign']!=$vars['userSign']){
                    unset($reDataArray[$key]);
                }
                if ($vars['eventType']&&$row['eventId']!=$vars['eventType']){
                    unset($reDataArray[$key]);
                }
                if ($vars['eventData01']&&$row['eventData01']!=$vars['eventData01']){
                    unset($reDataArray[$key]);
                }
            }
            
            //降序
            $reDataArray = count($reDataArray)>0 ? sysSortArray($reDataArray,'eventTime','SORT_DESC') : array();
            //显示记录
            $pageSize = 0;
            foreach ($reDataArray as $key => $row){
                $pageSize = $pageSize+1;
                if ($vars['pageSize']&&$pageSize>$vars['pageSize']){
                    unset($reDataArray[$key]);
                }
            }
            //排序
            $reDataArray = count($reDataArray)>0 ? sysSortArray($reDataArray,'eventTime',$vars['eventSort']) : array();
        }
        $this->view->eventTypeRows = $reEventRows;
        $this->view->rows = $reDataArray;
        $this->view->vars = $vars;

        //操作系统
        $pageVisitModel = new Application_Model_ActDayDataPagevisit();
        $osNameRows = $pageVisitModel ->getOsNameArray();
        $this->view->osNameRows = $osNameRows;
    }
    
    
    
    
    
    
    
}